<?php
/**
 * 系统设置
 */
class SystemApp extends CommonApp{
	
	public $fileList;

	public function indexAct(){
		$this->display();
	}
	
	// 返回随机字符串
	private function RandAbc($length = ""){
		$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		return str_shuffle($str); 
	}
	private function encodePhpCode($content){
		$T_k1 = self::RandAbc(); //随机密匙1
		$T_k2 = self::RandAbc(); //随机密匙2
		$v1 = base64_encode($content);
		$c = strtr($v1, $T_k1, $T_k2); //根据密匙替换对应字符。
		$c = $T_k1.$T_k2.$c;
		$q1 = "O00O0O";
		$q2 = "O0O000";
		$q3 = "O0OO00";
		$q4 = "OO0O00";
		$q5 = "OO0000";
		$q6 = "O00OO0";
		$result = '$'.$q6.'=urldecode("%6E1%7A%62%2F%6D%615%5C%76%740%6928%2D%70%78%75%71%79%2A6%6C%72%6B%64%679%5F%65%68%63%73%77%6F4%2B%6637%6A");$'.$q1.'=$'.$q6.'{3}.$'.$q6.'{6}.$'.$q6.'{33}.$'.$q6.'{30};$'.$q3.'=$'.$q6.'{33}.$'.$q6.'{10}.$'.$q6.'{24}.$'.$q6.'{10}.$'.$q6.'{24};$'.$q4.'=$'.$q3.'{0}.$'.$q6.'{18}.$'.$q6.'{3}.$'.$q3.'{0}.$'.$q3.'{1}.$'.$q6.'{24};$'.$q5.'=$'.$q6.'{7}.$'.$q6.'{13};$'.$q1.'.=$'.$q6.'{22}.$'.$q6.'{36}.$'.$q6.'{29}.$'.$q6.'{26}.$'.$q6.'{30}.$'.$q6.'{32}.$'.$q6.'{35}.$'.$q6.'{26}.$'.$q6.'{30};eval($'.$q1.'("'.base64_encode('$'.$q2.'="'.$c.'";eval(\'?>\'.$'.$q1.'($'.$q3.'($'.$q4.'($'.$q2.',$'.$q5.'*2),$'.$q4.'($'.$q2.',$'.$q5.',$'.$q5.'),$'.$q4.'($'.$q2.',0,$'.$q5.'))));').'"));';
		$result = '<?php '."\n".$result."\n".' ?>';
		return $result;
	}
	
	//过滤注释
	public function filterAct(){
		if(file_exists(PUB_DIR."share/plug/phpCB/lock"))
			exit("操作失败,项目已被锁定! (请删除插件目录下的lock文件phpCB)");
		//要处理的目录
		$folder = array(
		    'Application',
		    'Common/Dream',
		    'Common/Tool',
		    'Common/Payment',
		    'Common/Oauth',
		    'Library',
		);
		foreach($folder as $value){
			self::myscandir(DATA_DIR.'../'.$value);
		}
		//是否压缩代码
		$is_zip = (My::arr2var($_GET,'zip')=='true')?true:false;
		//print_r($this->fileList);
		$tempfile = DATA_DIR.'Cache/temp.php';//临时文件
		fopen($tempfile,'w');//没有该临时文件时进行创建
		if(is_array($this->fileList))
		foreach($this->fileList as $value){
			if(substr($value,-26)=='SystemController.class.php') continue;//当前文件不处理
			@chmod($value,0777);
			//加密代码
			if(My::arr2var($_GET,'type')=='encode'){
				$content = file_get_contents($value);
				$content = self::encodePhpCode($content);
				$file = fopen($value,"w");
				fwrite($file,$content);
			}else{
				//去除PHP源码中的回车、换行、空格和注释，达到代码压缩的效果
				$content = php_strip_whitespace($value);
				//添加版权
				$content = "<?php\n".C('admin|copyright').substr($content,5);
				$file = fopen($value,"w");
				fwrite($file,$content);
				//将压缩的代码格式化(当前只支持window系统)
				if(!$is_zip AND My::is_windows()){
					$cmd = PUB_DIR."share/plug/phpCB/phpCB.exe {$value} > {$tempfile}";
					system($cmd);//执行系统命令
					copy($tempfile, $value);
				}
			}
		}
		echo "成功";
	}
	//扫描目录
	private function myscandir($pathname){
		//$files = glob("*.{php,txt,js,htm}",GLOB_BRACE);
		foreach(glob($pathname) as $value )
		{
			if(is_dir($value)) {self::myscandir($value.'/*');}//目录时
			else {
				//只对php和js文件进行处理
				if(strpos($value,".php")!==FALSE OR strpos($value,".js")!==FALSE){ 
					$this->fileList[] = $value;
				}
			}
		}
	}

	/**
	 * 清缓存文件
	 */
	public function clearAct(){
		rm_dir(DATA_DIR.'Compile');//编译缓存
		//rm_dir(DATA_DIR.'File');//文件缓存
		rm_dir(DATA_DIR.'Field');//数据库模型缓存
		rm_dir(DATA_DIR.'Cache');//cache缓存
		//rm_dir(DATA_DIR.'Log');//日志缓存
		//rm_dir(DATA_DIR.'Session');//Session文件
		$this->ajaxReturn(200, '已清空所有缓存文件！');
	}
	
	/**
	 * 显示PHP配置信息
	 */
	public function phpinfoAct(){
		$_ENV = null;
		phpinfo();
	}
	
	/**
	 * 更新表映射
	 */
	public function updateTableMappendAct(){
		if($dbArr = import('Config.DbConfig', false, ROOT_DIR)){
			// 在更新ORM映射关系前清空所有的配置文件
			rm_dir(DATA_DIR.'Field'.__DS__);
			$dbStr = '';
			foreach($dbArr as $key=>$val){
				$ormM = DB('ORM','', $key);
				$dbStr .= $val['dbname'].',';
				if($tables = $ormM->tables()){
					foreach($tables as $table){
						$ormM->_getFields($table);
					}
				}
			}
			if($dbStr) $dbStr = rtrim($dbStr, ',');
			$this->ajaxReturn(200, "已更新数据库关系映射：【{$dbStr}】");
		}else{
			$this->ajaxReturn(300, '更新表映射失败!');
		}
	}
	
	/**
	 * 清空缓存数据
	 */
	public function clearCacheAct(){
		 rm_dir(DATA_DIR.'Cache');
		 $this->ajaxReturn(200, '已清空所有缓存文件！');
	}

}